<template>
  <div>

 <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-form :inline="true">
      <el-form-item>
        <el-input placeholder="请输入要搜索得商品" v-model="query" style="width:350px;">
          <el-button icon="el-icon-search" slot="append" @click="getUserList"></el-button>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="add_user_form = true">添加用户</el-button>
      </el-form-item>
    </el-form>

    <!-- 表单列表布局 -->
    <el-table style="width:100%" border :data="list">
      <el-table-column label="#" width="80" prop="id"></el-table-column>
      <el-table-column label="用户名" prop="username"></el-table-column>
      <el-table-column label="邮箱" prop="email"></el-table-column>
      <el-table-column label="电话" prop="mobile"></el-table-column>
      <el-table-column label="角色" prop="role_name"></el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <div>
            <el-switch
              @click="changeState(scope.row)"
              v-model="scope.row.mg_state"
              active-color="#0088dd"
              inactive-color="#dddddd"
            ></el-switch>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div>
            <el-button icon="el-icon-edit" type="primary" size="small" @click="edit(scope.row.id)"></el-button>
            <el-button
              icon="el-icon-delete"
              type="danger"
              size="small"
              @click="remove(scope.row.id)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        @size-change="changeSize"
        @current-change="changePage"
        :current-page="pagenum"
        :page-sizes="[2,4, 6, 8]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <el-dialog
      :title="isEdit == true ? '修改用户' : '添加用户'"
      :visible.sync="add_user_form"
      @close="closeForm"
    >
      <el-form label-width="80px" :model="users" ref="user_form" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input placeholder="请输入用户名" v-model="users.username" :disabled="isEdit" />
        </el-form-item>
        <el-form-item label="密码" prop="password" v-show="!isEdit">
          <el-input type="password" placeholder="请输入用密码" v-model="users.password" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input placeholder="请输入邮箱" v-model="users.email" />
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input placeholder="请输入电话" v-model="users.mobile" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="add_user_form = false">取 消</el-button>
        <el-button type="primary" @click="addUser" v-show="!isEdit">立即添加</el-button>
        <el-button type="success" @click="doEdit" v-show="isEdit">修改用户</el-button>
      </div>
    </el-dialog>

   
  </div>
</template>

<script>
export default {
  name: "",

  data() {
    return {
      //商品列表数据渲染
      list: [
        {
          id: 1,
          name: "张三",
          emall: "zs@qq.com",
          tel: "110",
          role: "地主",
          status: false
        }
      ],
      query: "", //查询参数
      pagenum: 1, //当前页码数
      pagesize: 2, //每页显示得条数
      total: 0, //总条数

      //添加用户
      add_user_form: false,
      users: {
        username: "",
        password: "",
        email: "",
        mobile: ""
      },
      rules: {
        username: [{ required: true, msg: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入电话", trigger: "blur" }]
      },
      //点击编辑得按钮修改用户
      isEdit: false
    };
  },
  mounted() {
    this.getUserList();
  },
  methods: {
    //   获取用户得列表数据
    getUserList() {
      let url = this.$store.state.baseUrl;
      this.$axios
        .get(
          `${url}users?query=${this.query}&pagenum=${this.pagenum}&pagesize=${this.pagesize}`
        )
        .then(res => {
          if (res.meta.status == 200) {
            this.list = res.data.users;
            this.total = res.data.total;
          }
        });
    },
    //分页
    changeSize(size) {
      this.pagesize = size;
      this.getUserList();
      this.pagenum = 1;
    },
    changePage(page) {
      this.pagenum = page;
      this.getUserList();
    },
    addUser() {
      this.$refs["user_form"].validate(check => {
        if (check) {
          //校验成功
          //请求添加用户的接口
          let url = this.$store.state.baseUrl; //请求接口的基地址
          this.$axios.post(`${url}users`, this.users).then(res => {
            console.log(res);
            if (res.meta.status == 201) {
              this.$message.success("创建用户成功");
              this.add_user_form = false; //关闭弹框
              this.getUserList(); //重新调用列表数据
            } else {
              this.$message.error(res.meta.msg); //失败的信息
            }
          });
        } else {
          //校验失败的时候
          this.$message.error("校验失败");
          return false;
        }
      });
    },
    //模糊搜索一下
    search() {
      this.pagenum = 1; //重置页码数
      this.getUserList();
    },
    //关闭模态框
    closeForm() {
      this.isEdit = false; //重置编辑状态
      //表单数据还原
      this.users = {
        username: "",
        password: "",
        email: "",
        mobile: ""
      };
    },

    //用户编辑的操作，数据回填
    edit(id) {
      //把编辑状态进行修改
      this.isEdit = true;
      this.add_user_form = true;

      //请求添加用户详情
      let url = this.$store.state.baseUrl; //请求接口的基地址
      this.$axios.get(`${url}users/${id}`).then(res => {
        console.log(res.data);
        this.users = res.data;
      });
    },

    //执行编辑的操作
    doEdit() {
      //获取到要修改的用户的id值
      let id = this.users.id;

      //要修改的用户数据
      let form = {
        email: this.users.email,
        mobile: this.users.mobile
      };
      //请求修改用户的接口地址
      let url = this.$store.state.baseUrl; //请求接口的基地址
      this.$axios.put(`${url}users/${id}`, form).then(res => {
        console.log(res.data);
        if (res.meta.status == 200) {
          this.getUserList(); //重新获取用户的列表数据
          this.closeForm(); // 重置模态框状态
          this.add_user_form = false;
          this.$message.success("修改用户成功");
        } else {
          this.$message.error(res.meta.msg);
        }
      });
    },
    //修改用户状态

    changeState(row) {
      let url = this.$store.state.baseUrl; //请求接口的基地址
      this.$axios
        .put(`${url}users/${row.id}/state/${row.mg_state}`)
        .then(res => {
          console.log(res);
          if (res.meta.status == 200) {
            this.$message.success(res.meta.msg);
            this.getUserList();
          } else {
            this.$message.error(res.meta.msg);
          }
        });
    },
    //删除方法
    remove(id) {
      this.$confirm("你确定要删除用户吗,删除就不能恢复了吆", "删除提示")
        .then(() => {
          //请求修改状态的接口
          let url = this.$store.state.baseUrl; //请求接口的基地址

          this.$axios.delete(`${url}users/${id}`).then(res => {
            console.log(res);
            if (res.meta.status == 200) {
              this.$message.success("删除用户成功");
              this.getUserList();
            } else {
              this.$message.error(res.meta.msg);
            }
          });
        })
        .catch(() => {
          this.$message("你拒绝了我");
        });
    }
  },

  computed: {},
  watch: {}
};
</script>

<style>
.page {
  margin-top: 20px;
}
.el-breadcrumb{
  margin-bottom: 30px;
}
</style>